﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace NLBS.Text
{
    /// <summary>
    /// 控件辅助类
    /// </summary>
    public class ControlHelper
    {
        /// <summary>
        /// Appends a CSS class to a control.
        /// <br />
        /// 为控件添加CSS样式
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="newClass">The new class.</param>
        public static void AppendCssClass(WebControl control, string newClass)
        {
            if (control == null)
                throw new ArgumentNullException("Cannot add a css class to a null control");

            if (newClass == null)
                throw new ArgumentNullException("Cannot add a null css class to a control");

            string existingClasses = control.CssClass;
            if (String.IsNullOrEmpty(existingClasses))
            {
                control.CssClass = newClass;
                return;
            }

            string[] classes = existingClasses.Split(' ');
            foreach (string attributeValue in classes)
            {
                if (String.Equals(attributeValue, newClass, StringComparison.Ordinal))
                {
                    //value's already in there.
                    return;
                }
            }
            control.CssClass += " " + newClass;
        }

        /// <summary>
        /// Removes a CSS class to a control.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="classToRemove">The new class.</param>
        public static void RemoveCssClass(WebControl control, string classToRemove)
        {
            
            if (control == null)
                throw new ArgumentNullException("Cannot remove a css class from a null control");

            if (classToRemove == null)
                throw new ArgumentNullException("Cannot remove a null css class from a control");

            string existingClasses = control.CssClass;
            if (String.IsNullOrEmpty(existingClasses))
                return; //nothing to remove

            string[] classes = existingClasses.Split(new string[] { " ", "\t", "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
            string newClasses = string.Empty;
            foreach (string cssClass in classes)
            {
                if (!String.Equals(cssClass, classToRemove, StringComparison.Ordinal))
                {
                    newClasses += cssClass + " ";
                }
            }

            if (newClasses.EndsWith(" "))
                newClasses = newClasses.Substring(0, newClasses.Length - 1);
            control.CssClass = newClasses;
        }

        /// <summary>
        /// Appends the attribute value to the control appropriately.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void AppendAttributeValue(WebControl control, string name, string value)
        {
            string existingValue = control.Attributes[name];
            if (String.IsNullOrEmpty(existingValue))
            {
                control.Attributes[name] = value;
                return;
            }
            else
            {
                string[] attributeValues = control.Attributes[name].Split(' ');
                foreach (string attributeValue in attributeValues)
                {
                    if (String.Equals(attributeValue, value, StringComparison.Ordinal))
                    {
                        //value's already in there.
                        return;
                    }
                }
                control.Attributes[name] += " " + value;
            }
        }
    }
}
